<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Models\FailedJobs;
use App\Models\Jobs;
use App\Models\Admin\Setting;
use Illuminate\Http\Request;
use App\Jobs\SendMail;
use Illuminate\Support\Facades\Artisan;
use Illuminate\Support\Facades\Log;

class QueueController extends Controller
{
    public function index()
    {
        global $_G;
        $res = ['title' => '我的'];
        //$res['user'] = Auth::user();
        //dispatch(new SendMail('xx'));
        //Log::info('test');
        $res['data'] = Jobs::Paginate($_G['perPage']);
        return view('admin.queue.index', ['res' => $res]);
    }

    public function failed()
    {
        global $_G;
        $res = ['title' => '我的'];
        //$res['user'] = Auth::user();
        $res['data'] = FailedJobs::Paginate($_G['perPage']);
        return view('admin.queue.failed', ['res' => $res]);
    }

    public function show()
    {
        global $_G;
        $res = ['title' => '我的'];
        //$res['user'] = Auth::user();
        $res['data'] = FailedJobs::Paginate($_G['perPage']);
        return view('admin.queue.failed', ['res' => $res]);
    }

    public function set(Request $request)
    {
        $res=['title'=>'我的'];
        //$res['user']=Auth::user();
        if($request->isMethod('post')) {
            $data['QUEUE_CONNECTION'] = $request->input('queue');
            Setting::modifyEnv($data);
            return redirect()->route('admin.queue')->with('status', 'success');
        }else{
            $res['mod'] = env('QUEUE_CONNECTION');
            return view('admin.queue.set',['res'=>$res]);
        }
    }

    public function work()
    {
        Artisan::call('queue:work');
        return 'ok';
    }

    public function failflush()
    {
        Artisan::call('queue:flush');
    }

    public function add(Request $request)
    {
        dispatch(new SendMail('asd'));
    }
}
